#include "EffectManager.h"
#include "Effect.h"
#include "EffectDefine.h"
#include "EffectFactory.h"
#include "MainLayer.h"


EffectManager::EffectManager(void)
{
	_listEffect = NULL;
}


EffectManager::~EffectManager(void)
{
	CC_SAFE_RELEASE(_listEffect);
}

bool EffectManager::init()
{
	if (!CCNode::init())
		return false;

	setEffects(CCArray::create());

	return true;
}

void EffectManager::onEnter()
{
	CCNode::onEnter();
	schedule(SEL_SCHEDULE(&EffectManager::effectProcessing));
}

void EffectManager::onExit()
{
	CCNode::onExit();
	unschedule(SEL_SCHEDULE(&EffectManager::effectProcessing));
}

void EffectManager::addEffect( Effect* effect )
{
	_listEffect->addObject(effect);
	getMainLayer()->addChild(effect);
}

void EffectManager::removeEffect( Effect* effect )
{
	if (!effect)
		return;
	_listEffect->removeObject(effect);
	getMainLayer()->removeChild(effect);
}


MainLayer* EffectManager::getMainLayer() 
{
	return dynamic_cast<MainLayer*>(getParent());
}

void EffectManager::effectProcessing( float dt )
{
	CCObject* it;
	CCArray* listToDelete = CCArray::create();
	CCARRAY_FOREACH(_listEffect, it)
	{
		Effect* ef = dynamic_cast<Effect*>(it);
		if (ef->isDone())
		{
			listToDelete->addObject(ef);
		}
	}
	CCARRAY_FOREACH(listToDelete, it)
	{
		removeEffect(dynamic_cast<Effect*>(it));
	}
}
